package com.xsky.logic.service.impl;

import java.io.IOException;

import org.dom4j.DocumentException;

import com.xsky.common.logic.BillNoGenerator;
import com.xsky.common.util.UUIDUtil;
import com.xsky.logic.LogonLogic;
import com.xsky.logic.agreement.AgreementLogic;
import com.xsky.logic.annonce.AnnonceLogic;
import com.xsky.logic.casehistory.CaseOperateHistoryLogic;
import com.xsky.logic.casetype.CaseTypeLogic;
import com.xsky.logic.client.ClientLogic;
import com.xsky.logic.company.CompanyLogic;
import com.xsky.logic.department.DepartmentLogic;
import com.xsky.logic.engineer.EngineerLogic;
import com.xsky.logic.equiptype.EquipTypeLogic;
import com.xsky.logic.groupmessage.GroupMessageLogic;
import com.xsky.logic.knowledge.KnowledgeLogic;
import com.xsky.logic.library.LibraryDirLogic;
import com.xsky.logic.library.LibraryLogic;
import com.xsky.logic.platfault.PlatFaultLogic;
import com.xsky.logic.role.RoleLogic;
import com.xsky.logic.service.facade.ICesiService;
import com.xsky.logic.tabcase.CaseLogic;
import com.xsky.logic.upgrade.UpgrandeLogic;

public class CesiService implements ICesiService {

//登录相关
	public String logon(String username, String password)
	{
		LogonLogic logic = new LogonLogic();
		return logic.logon(username, password);
	}

	public String getNewBillNo() {
		return BillNoGenerator.getBillNo();
	}

	public String getNewUUID()
	{
		return UUIDUtil.getUUID();
	}

	// 公司相关
	public String addCompany(String xmlStr) {
		CompanyLogic logic = new CompanyLogic();
		return logic.add(xmlStr);
	}

	public String updateCompany(String xmlStr) {
		CompanyLogic logic = new CompanyLogic();
		return logic.update(xmlStr);
	}

	public String deleteCompany(String xmlStr)
	{
		CompanyLogic logic = new CompanyLogic();
		return logic.delete(xmlStr);
	}
	public String deleteCompanyById(String id)
	{
		CompanyLogic logic = new CompanyLogic();
		return logic.deleteById(id);
	}
	public String genAllCompany()
	{
		CompanyLogic logic = new CompanyLogic();
		return logic.genAll();
	}

	// 部门相关
	public String addDepartment(String xmlStr) {
		DepartmentLogic dl = new DepartmentLogic();
		dl.add(xmlStr);
		return "";
	}

	public String genDepartmentTree(String companyId) {
		DepartmentLogic dl = new DepartmentLogic();
		String rootId = dl.genTreeRootId(companyId);
		return "<data>" + dl.genTree(rootId) + "</data>";
	}
	public String deleteDepartment(String xmlStr)
	{
		DepartmentLogic dl = new DepartmentLogic();
		return dl.delete(xmlStr);
	}
	public String deleteDepartmentById(String id)
	{
		DepartmentLogic dl = new DepartmentLogic();
		return dl.delete(id);
	}

	public String updateDepartment(String xmlStr) {
		DepartmentLogic dl = new DepartmentLogic();
		return dl.update(xmlStr);
	}

	public String isEmptyDepartment(String id)// 判断某部门下是否具有子部门
	{
		DepartmentLogic dl = new DepartmentLogic();
		return dl.isEmpty(id);
	}

	// 协议相关
	public String addAgreement(String xmlStr) {
		AgreementLogic logic = new AgreementLogic();
		return logic.add(xmlStr);
	}

	public String genAllAgreement() {
		AgreementLogic logic = new AgreementLogic();
		return logic.genAll();
	}

	public String genAgreementWithCondition(String condition) {
		AgreementLogic logic = new AgreementLogic();
		return logic.genWithCondition(condition);
	}

	public String genAgreementForClient(String condition) {
		AgreementLogic logic = new AgreementLogic();
		return logic.genForClient(condition);
	}

	public String deleteAgreement(String xmlStr) {
		AgreementLogic logic = new AgreementLogic();
		return logic.deleteSelected(xmlStr);
	}
	public String deleteAgreementById(String id)
	{
		AgreementLogic logic = new AgreementLogic();
		return logic.delete(id);
	}
	//工程师相关
	public String addEngineer(String xmlStr)
	{
		EngineerLogic logic = new EngineerLogic();
		return logic.add(xmlStr);
	}

	public String genAllEngineer() {
		EngineerLogic logic = new EngineerLogic();
		return logic.genAll();
	}

	public String genEngineerWithCondition(String condition) {
		EngineerLogic logic = new EngineerLogic();
		return logic.genWithCondition(condition);
	}
	public String deleteEngineerById(String id)
	{
		EngineerLogic logic = new EngineerLogic();
		return logic.deleteById(id);
	}
	public String genEngineerWithConditionForSearch(String condition)
	{
		EngineerLogic logic = new EngineerLogic();
		return logic.genWithConditionForSearch(condition);
	}
	public String deleteEngineer(String xmlStr) {
		EngineerLogic logic = new EngineerLogic();
		return logic.delete(xmlStr);
	}
//	Case描述相关
	public String addOrUploadCaseDescAndResolves(String xml)
	{
		return new CaseLogic().addOrUploadCaseDescAndResolves(xml);
	}

	public String queryCaseAndResolves(String caseId)
	{
		return new CaseLogic().queryCaseAndResolves(caseId);
	}


	public String addOrUpdateCase(String xml)
	{
		return new CaseLogic().addOrUpdateCase(xml);
	}
	
	

	public String queryCase(String xml)
	{
		return new CaseLogic().queryCase(xml);
	}

	public String addOrUpdateDispatch(String xml)
	{
		return new CaseLogic().addOrUpdateDispatch(xml);
	}

	public String getStatInfo() {
		return new CaseLogic().getStatInfo();
	}
	public String addCaseOperateHistory(String xml){
		return new CaseOperateHistoryLogic().addOrUpdateCaseOperateHistory(xml);
	}

	public String queryCaseOperateHistory(String caseId) {
		return new CaseOperateHistoryLogic().queryHistoryXML(caseId);
	}

	// Case类型相关
	public String addCaseType(String xmlStr) {
		CaseTypeLogic dl = new CaseTypeLogic();
		dl.add(xmlStr);
		return "";
	}

	public String genCaseTypeTree() {
		CaseTypeLogic dl = new CaseTypeLogic();
		String rootId = dl.genTreeRootId();
		return "<data>" + dl.genTree(rootId) + "</data>";
	}

	public String deleteCaseType(String id) {
		CaseTypeLogic dl = new CaseTypeLogic();
		return dl.delete(id);
	}

	public String updateCaseType(String xmlStr) {
		CaseTypeLogic dl = new CaseTypeLogic();
		return dl.update(xmlStr);
	}

	public String isEmptyCaseType(String id)// 判断某部门下是否具有子部门
	{
		CaseTypeLogic dl = new CaseTypeLogic();
		return dl.isEmpty(id);
	}

	// 客户相关
	public String addClient(String xmlStr) {
		ClientLogic logic = new ClientLogic();
		return logic.add(xmlStr);
	}

	public String genAllClient() {
		ClientLogic logic = new ClientLogic();
		return logic.genAll();
	}

	public String genClientWithCondition(String condition) {
		ClientLogic logic = new ClientLogic();
		return logic.genWithCondition(condition);
	}
	public String deleteClient(String xmlStr)
	{
		ClientLogic logic = new ClientLogic();
		return logic.delete(xmlStr);
	}
	public String deleteClientById(String id)
	{
		ClientLogic logic = new ClientLogic();
		return logic.deleteById(id);
	}

	public String updateClient(String xmlStr) {
		ClientLogic logic = new ClientLogic();
		return logic.update(xmlStr);
	}

	// 数据字典相关
	public String addLibraryDir(String xmlStr) {
		LibraryDirLogic logic = new LibraryDirLogic();
		return logic.add(xmlStr);
	}

	public String genAllLibraryDir() {
		LibraryDirLogic logic = new LibraryDirLogic();
		return logic.genAll();
	}

	public String genLibraryDirWithCondition(String condition) {
		LibraryDirLogic logic = new LibraryDirLogic();
		return logic.genWithCondition(condition);
	}

	public String deleteLibraryDir(String id) {
		LibraryDirLogic dl = new LibraryDirLogic();
		return dl.delete(id);
	}
	public String deleteLibraryDirById(String id)
	{
		LibraryDirLogic dl = new LibraryDirLogic();
		return dl.deleteById(id);
	}
	public String updateLibraryDir(String xmlStr)
	{
		LibraryDirLogic dl = new LibraryDirLogic();
		return dl.update(xmlStr);
	}

	public String addLibrary(String xmlStr) {
		LibraryLogic logic = new LibraryLogic();
		return logic.add(xmlStr);
	}

	public String genAllLibrary(String parentId) {
		LibraryLogic logic = new LibraryLogic();
		return logic.genAll(parentId);
	}

	public String genLibraryWithCondition(String condition) {
		LibraryLogic logic = new LibraryLogic();
		return logic.genWithCondition(condition);
	}

	public String deleteLibrary(String id) {
		LibraryLogic dl = new LibraryLogic();
		return dl.delete(id);
	}
	public String deleteLibraryById(String id)
	{
		LibraryLogic dl = new LibraryLogic();
		return dl.deleteById(id);
	}
	public String updateLibrary(String xmlStr)
	{
		LibraryLogic dl = new LibraryLogic();
		return dl.update(xmlStr);
	}

	public String genFromLibrary(String type) {
		LibraryLogic dl = new LibraryLogic();
		return dl.genByType(type);
	}

	// 系统公告相关
	public String addNotice(String xmlStr) {
		AnnonceLogic logic = new AnnonceLogic();
		return logic.add(xmlStr);
	}

	public String genAllNotice() {
		AnnonceLogic logic = new AnnonceLogic();
		return logic.genAll();
	}

	public String genNoticeWithCondition(String condition) {
		AnnonceLogic logic = new AnnonceLogic();
		return logic.genWithCondition(condition);
	}
	public String deleteNotice(String xmlStr)
	{
		AnnonceLogic dl = new AnnonceLogic();
		return dl.delete(xmlStr);
	}
	public String deleteNoticeById(String id)
	{
		AnnonceLogic dl = new AnnonceLogic();
		return dl.deleteById(id);
	}

	public String updateNotice(String xmlStr) {
		AnnonceLogic dl = new AnnonceLogic();
		return dl.update(xmlStr);
	}
//	角色权限相关
	public String addRole(String xmlStr)
	{
		RoleLogic logic = new RoleLogic();
		return logic.add(xmlStr);
	}
	public String genAllRole()
	{
		RoleLogic logic = new RoleLogic();
		return logic.genAll();
	}
	public String genRoleWithCondition(String condition)
	{
		RoleLogic logic = new RoleLogic();
		return logic.genWithCondition(condition);
	}
	public String deleteRoleById(String id)
	{
		RoleLogic dl = new RoleLogic();
		return dl.deleteById(id);
	}
	public String deleteRole(String xmlStr)
	{
		RoleLogic dl = new RoleLogic();
		return dl.delete(xmlStr);
	}
	public String updateRole(String xmlStr)
	{
		RoleLogic dl = new RoleLogic();
		return dl.update(xmlStr);
	}
	public String genUserWithRole(String roleId)
	{
		RoleLogic dl = new RoleLogic();
		return dl.genUserWithRole(roleId);
	}
	public String genUsersWithRole(String roleId)
	{
		RoleLogic dl = new RoleLogic();
		return dl.genUsers(roleId);
	}
	public String addUserRole(String roleId,String users)
	{
		RoleLogic dl = new RoleLogic();
		return dl.addUserRole(roleId,users);
	}
	public String deleteUserRole(String roleId,String userId)
	{
		RoleLogic dl = new RoleLogic();
		return dl.deleteUserRole(roleId,userId);
	}
	public String deleteUserRoleById(String roleId,String userId)
	{
		RoleLogic dl = new RoleLogic();
		return dl.deleteUserRole(roleId,userId);
	}
	public String deleteUserRoles(String userXml)
	{
		RoleLogic dl = new RoleLogic();
		return dl.deleteUserRoles(userXml);
	}
	public String genPowerInfo(String roleId)
	{
		RoleLogic dl = new RoleLogic();
		try
		{
			return dl.genPowerInfo(roleId);
		} catch (DocumentException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public String addAccess(String accessXml)
	{
		RoleLogic dl = new RoleLogic();
		return dl.addAccess(accessXml);
	}
	public String addRoleAccess(String roleId,String accessXml)
	{
		RoleLogic dl = new RoleLogic();
		return dl.addRoleAccess(roleId, accessXml);
	}
	
	//群发设置
	public String addMessageGroup(String xmlStr)
	{ 
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.add(xmlStr);
	}
	public String genAllMessageGroup()
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.genAll();
		
	}
	public String genAllUserOfMessageGroup(String users)
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.genUsers(users);
	}
	public String genUserNotInMessageGroup(String users)
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.genUsersNotInGroup(users);
	}
	public String addMessageGroupUser(String groupId,String userXml)
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.addUserGroup(groupId, userXml);
	}
	public String deleteMessageGroupUser(String userId)
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.deleteMessageGroupUser(userId);
	}
	public String deleteMessageGroupUserById(String groupId,String userId)
	{
		GroupMessageLogic logic = new GroupMessageLogic();
		return logic.deleteMessageGroupUserById(groupId, userId);
	}
	//升级策略相关
	public String addUpgradeSetting(String xmlStr)
	{
		UpgrandeLogic logic= new UpgrandeLogic();
		return logic.add(xmlStr);
	}
	public String genAllUpgradeSetting()
	{
		UpgrandeLogic logic= new UpgrandeLogic();
		return logic.genAll();
	}
	public String deleteUpgradeSetting(String xmlStr)
	{
		UpgrandeLogic logic= new UpgrandeLogic();
		return logic.delete(xmlStr);
	}
	public String deleteUpgradeSettingById(String id)
	{
		UpgrandeLogic logic= new UpgrandeLogic();
		return logic.deleteById(id);
	}

	// 知识库相关
	public String addKnowledge(String xml) {
		return new KnowledgeLogic().add(xml);
	}

	public String deleteKnowledge(String id) {
		return new KnowledgeLogic().delete(id);
	}

	public String getKnowledgeHistorys(String id) {
		return new KnowledgeLogic().queryHistory(id);
	}

	public String getKnowledges(String xml) {
		return new KnowledgeLogic().queryByCondition(xml);
	}

	public String updateKnowledge(String xml) {
		return new KnowledgeLogic().update(xml);
	}

	public String addPlatFault(String xml) {
		return new PlatFaultLogic().add(xml);
	}

	public String deletePlatFault(String id) {
		return new PlatFaultLogic().delete(id);
	}
	

	public String queryPlatFault(String xml) {
		return new PlatFaultLogic().queryByCondition(xml);
	}

	public String getCountOfCurMonth() {
		return new PlatFaultLogic().getCountOfCurMonth();
	}

	public String getCountOfCurYear() {
		return new PlatFaultLogic().getCountOfCurYear();
	}

	public String updatePlatFault(String xml) {
		return new PlatFaultLogic().update(xml);
	}
//	设备类型相关
	public String addEquipType(String xmlStr)
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		dl.add(xmlStr);
		return "";
	}
	public String genEquipTypeTree()
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		String rootId = dl.genTreeRootId();
		return "<data>"+dl.genTree(rootId)+"</data>";
	}
	public String deleteEquipType(String id)
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		return dl.delete(id);
	}
	public String updateEquipType(String xmlStr)
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		return dl.update(xmlStr);
	}
	public String isEmptyEuipType(String id)//
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		return dl.isEmpty(id);
	}
	public String genUpperEquipType(String id)
	{
		EquipTypeLogic dl = new EquipTypeLogic();
		String equipTypeStr = dl.genUpper(id);
		String[] equipTypeArr = equipTypeStr.split("->");
		equipTypeStr = "";
		for(int i = equipTypeArr.length-1; i>=0; i--)
		{
			equipTypeStr+="->"+equipTypeArr[i];
		}
		equipTypeStr=equipTypeStr.replace("->根节点->", "");
		return equipTypeStr;
	}
}